package com.sdcqjy.property.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.CommentAdapter;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.mode.CommentMode;
import com.sdcqjy.property.presenter.CommentPresenter;
import com.sdcqjy.property.presenter.contract.CommentContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/12/4.
 * 评论
 */
public class CommentFragment extends BaseFragment<CommentPresenter> implements CommentContract.View
        , OnRefreshListener, OnLoadmoreListener {
    @BindView(R.id.listView)
    RecyclerView listView;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;
    @BindView(R.id.editContent)
    EditText editContent;
    @BindView(R.id.btnSend)
    TextView btnSend;
    @BindView(R.id.layoutEditRoot)
    LinearLayout layoutEditRoot;
    Unbinder unbinder;
    private String id;
    private List<CommentMode> modeList;
    private CommentAdapter adapter;

    public static BaseFragment newInstance(String id) {
        CommentFragment fragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    public CommentFragment() {
        presenter = new CommentPresenter(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        id = bundle.getString("id");
        modeList = new ArrayList<>();
        adapter = new CommentAdapter(getActivity(), modeList);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listView.addItemDecoration(new SpacesItemDecoration(2, new boolean[]{false, false, false, true}));
        smartLayout.setOnRefreshListener(this);
        smartLayout.setOnLoadmoreListener(this);
        smartLayout.setEnableLoadmore(false);
        smartLayout.autoRefresh();
    }

    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void applyError(String t) {
        smartLayout.finishRefresh();
        smartLayout.finishLoadmore();
        super.applyError(t);
    }

    private int page = 1;

    public void onRefresh(RefreshLayout refreshlayout) {
        smartLayout.setLoadmoreFinished(false);
        page = 1;
        presenter.getCommentList(id, page);
    }

    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        presenter.getCommentList(id, page);
    }

    public void getCommentListRet(List<CommentMode> data) {
        smartLayout.finishRefresh();
        smartLayout.finishLoadmore();
        List<CommentMode> list = data;
        if (list == null) list = new ArrayList<>();
        //smartLayout.setLoadmoreFinished(list.size() == 0 || data.is_end_page);
        if (page == 1) modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.btnSend)
    public void onBtnSendClicked() {
        if (!isLogin()) {
            return;
        }
        String s = editContent.getText().toString();
        if (TextUtils.isEmpty(s)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrpl);
            return;
        }
        presenter.sendComment(id, s);
    }

    public void sendCommentSucc() {
        editContent.setText("");
        smartLayout.autoRefresh();
    }
}
